package com.hpin.fansea.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.CircleClaBean;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.DensityUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 *
 * 商圈分类adapter
 * Created by Aaron on 15/10/22.
 */
public class CircleClassifyAdapter extends BaseAdapter {

    private Context ctx;
    private DisplayImageOptions options;
    private List<CircleClaBean.CircleResult> circleResults;

    public CircleClassifyAdapter(Context ctx, List<CircleClaBean.CircleResult> circleResults) {
        this.ctx = ctx;
        this.circleResults = circleResults;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_fail_big_default)
                .showImageForEmptyUri(R.mipmap.icon_fail_big_default)
                .showImageOnFail(R.mipmap.icon_fail_big_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();
    }

    public void setCircleResults(List<CircleClaBean.CircleResult> circleResults) {
        this.circleResults = circleResults;
    }

    @Override
    public int getCount() {
        return circleResults.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder=null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_circle_waterfull, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_style = (TextView) convertView.findViewById(R.id.tv_style);
            viewHolder.tv_green = (TextView) convertView.findViewById(R.id.tv_green);
            viewHolder.tv_first = (TextView) convertView.findViewById(R.id.tv_first);
            viewHolder.tv_old_price = (TextView) convertView.findViewById(R.id.tv_old_price);
            viewHolder.tv_now_price = (TextView) convertView.findViewById(R.id.tv_now_price);
            viewHolder.rl_img_bg = (RelativeLayout) convertView.findViewById(R.id.rl_img_bg);
            convertView.setTag(viewHolder);

        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

        viewHolder.tv_style.setText(circleResults.get(position).catNm);
        if (CommonUtils.isNull(circleResults.get(position).tag)) {
            viewHolder.tv_green.setVisibility(View.INVISIBLE);
        }else {
            viewHolder.tv_green.setVisibility(View.VISIBLE);
            viewHolder.tv_green.setText(circleResults.get(position).tag);
        }
        viewHolder.tv_first.setText(circleResults.get(position).groupName);
        viewHolder.tv_now_price.setText(circleResults.get(position).tgPr);
        viewHolder.tv_old_price.setText(circleResults.get(position).salePr);
        viewHolder.tv_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线

        if (position==0)
            viewHolder.rl_img_bg.setBackgroundResource(R.mipmap.icon_circle_travel);
//            ImageLoader.getInstance().displayImage("http://img.hao123.com/data/1_09d3bd2b43bdce6eced7057a93f4516c_0", viewHolder.iv_bg, options);
        else if (position==1)
//            ImageLoader.getInstance().displayImage("http://img0.hao123.com/data/1_65b46345b244b38545fbd370a133bc18_510", viewHolder.iv_bg, options);
            viewHolder.rl_img_bg.setBackgroundResource(R.mipmap.icon_circle_group);
        else if (position==2)
//            ImageLoader.getInstance().displayImage("http://img0.hao123.com/data/1_7676fab2a8dd3b00576e56efaaf09d18_510", viewHolder.iv_bg, options);
            viewHolder.rl_img_bg.setBackgroundResource(R.mipmap.icon_circle_play);
       else  if (position==3)
            viewHolder.rl_img_bg.setBackgroundResource(R.mipmap.icon_circle_food);
//            ImageLoader.getInstance().displayImage("http://img4.hao123.com/data/1_4b09f10892fe1b91b9d05a53c809af5a_510", viewHolder.iv_bg, options);

//        viewHolder.iv_bg.setText(cityList.get(position).cityName);

        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                DensityUtil.dip2px(ctx, 163), DensityUtil.dip2px(ctx, 163));
        convertView.setLayoutParams(param);

//        final int p = position;

//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                if ("娱乐".equals(circleResults.get(p).catNm)
//                        || "美食".equals(circleResults.get(p).catNm))  {
//
//                    Toast.makeText(ctx, "暂未开放", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                Intent mIntent = new Intent(ctx, GoodsActivity.class);
//                mIntent.putExtra("categoryId", circleResults.get(p).id_);
//                mIntent.putExtra("categoryName", circleResults.get(p).catNm);
//                ctx.startActivity(mIntent);
//
//            }
//        });

        return convertView;
    }

    private class ViewHolder {
        public RelativeLayout rl_img_bg;
        public TextView tv_style;
        public TextView tv_green;
        public TextView tv_first;
        public TextView tv_old_price;
        public TextView tv_now_price;
    }
}
